<template>
  <div>
    <p-modal
      title="Title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >


        <!-- <p-form-model-item
          ref="workroomname"
          label="车间"
          prop="workroomname"
        >
          <p-input v-model="form.workroomname" />
        </p-form-model-item>

        <p-form-model-item
          ref="linename"
          label="线体"
          prop="linename"
        >
          <p-input v-model="form.linename" />
        </p-form-model-item> -->


        <p-form-model-item ref="lineid" label="线体" prop="lineid">

          <p-select v-model="form.lineid" @change="lineChange">
            <p-select-option v-for="(item, index) in proLineList" :key="index" :value="item.id+''">
              {{item.wpname}}
            </p-select-option>
          </p-select>

        </p-form-model-item>


        <p-form-model-item ref="workroomname" label="车间" prop="workroomname">
          <p-input
            disabled
            v-model="form.workroomname"
          />
        </p-form-model-item>

        <p-form-model-item
          ref="changeLineTimes"
          label="换线次数"
          prop="changeLineTimes"
        >
          <p-input v-model="form.changeLineTimes" />
        </p-form-model-item>

        <p-form-model-item
          ref="moneyhourGive"
          label="补贴工时H/每工站"
          prop="moneyhourGive"
        >
          <p-input v-model="form.moneyhourGive"/>
        </p-form-model-item>

        <p-form-model-item ref="remark" label="备注" prop="remark">
          <p-input v-model="form.remark" />
        </p-form-model-item>
      </p-form-model>
    </p-modal>
  </div>
</template>
<script>
import { postAction, getAction, putAction, deleteAction } from "@/api/manage";

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 12 },
      state: "add",
      form: {
        workroomname: '',
        lineid: "",
        changeLineTimes: "",
        moneyhourGive: "",
        remark: "",
      },
      rules: {
        workroomname: [
          { required: true, message: "请输入车间", trigger: "blur" },
        ],
        lineid: [
          { required: true, message: "请选择线体", trigger: "blur" },
        ],
        changeLineTimes: [
          { required: true, message: "请输入换线次数", trigger: "blur" },
        ],
        moneyhourGive: [
          { required: true, message: "请输入补贴工时H/每工站", trigger: "blur" },
        ],
      },
      proLineList: [],
   
      url: {
        add: "/mes-manpower/mesHrCutlineLossCoefficient",
        edit: "/mes-manpower/mesHrCutlineLossCoefficient",
        proLineList: '/basic-business/mesBaArchitecture/proLineList'  // 线体列表
      },
    };
  },
  mounted() {
    this.getProLineList()
  },
  methods: {

    // 获取线体列表
    getProLineList(){
      getAction(this.url.proLineList).then( res => {
        if(res.success) {
          this.proLineList = res.data
        }
      } )
    },

    // 选择线体
    lineChange(val){
      this.proLineList.forEach( ele => {
        if(val == ele.id) {
          this.form.workroomid = ele.fwpid
          this.form.workroomname = ele.fwpname
          this.form.workroomno = ele.fwpno
          this.form.linename = ele.wpname
          this.form.lineno = ele.wpno
        }
      } )
    },

    showModal(record) {
      if (record) {
        this.state = "edit";
        for (let key in this.form) {
          this.form[key] = record[key];
        }
        this.editId = record.id;
      } else {
        this.state = "add";
      }

      this.visible = true;
    },
    handleOk(e) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.form));
          this.confirmLoading = true;
          if (this.state == "add") {
            postAction(this.url.add, params).then((res) => {
              if (res.success) {
                this.handleCancel()
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }

          if (this.state == "edit") {
            params.id = this.editId;
            putAction(this.url.edit, params).then((res) => {
              if (res.success) {
                this.handleCancel()
                this.$emit("handleSuccess");
              } else {
                this.$message.error(res.msg);
              }
              this.confirmLoading = false;
            });
          }
        }
      });
    },
    handleCancel(e) {
      this.visible = false;
      Object.assign(this.form, this.$options.data().form);
      this.$refs.ruleForm.resetFields()
    },
  },
};
</script>
